<template>
  <el-dialog :visible.sync="visible" width="600px" :title="!dataForm.id ? $t('transfer') : $t('transfer')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-row>
        <el-col :span="12">
          <el-form-item size='small' label="原猪舍" prop="preHouseId">
            <el-select v-model="dataForm.preHouseId" @change="house_change" placeholder="原猪舍">
              <el-option
                  v-for="item in houses"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item size='small' label="原栏位" prop="preGrid">
            <el-select size='mini' style="width: 90px" v-model="dataForm.preGrid.row" placeholder="行">
              <el-option
                v-for="item in rows"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-select size='mini' style="width: 90px" v-model="dataForm.preGrid.column" placeholder="列">
              <el-option
                v-for="item in columns"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item size='small' label="新猪舍" prop="toHouseId">
            <el-select v-model="dataForm.toHouseId" @change="house_change2" placeholder="新猪舍">
              <el-option
                  v-for="item in houses"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item size='small' label="新栏位" prop="toGrid">
            <el-select size='mini' style="width: 90px" v-model="dataForm.toGrid.row" placeholder="行">
              <el-option
                v-for="item in rows2"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-select size='mini' style="width: 90px" v-model="dataForm.toGrid.column" placeholder="列">
              <el-option
                v-for="item in columns2"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item size='small' label="转舍日期" prop="transferDate">
            <el-date-picker
              v-model="dataForm.transferDate"
              align="right"
              type="date"
              placeholder="转舍日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item size='small' label="数量" prop="mounts">
            <el-input v-model="dataForm.mounts" placeholder="数量" type="number"><i slot="suffix" style="font-style:normal;margin-right: 10px;">头</i></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item size='small' label="转舍日龄" prop="age">
            <el-input v-model="dataForm.age" placeholder="日龄" type="number"><i slot="suffix" style="font-style:normal;margin-right: 10px;">天</i></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item size='small' label="重量" prop="weight">
            <el-input v-model="dataForm.weight" placeholder="重量" type="number"><i slot="suffix" style="font-style:normal;margin-right: 10px;">kg</i></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item size='small' label="操作人" prop="operator">
            <UserSelect v-model="dataForm.operator"></UserSelect>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item size='small' label="备注" prop="remark">
        <el-input type="textarea" :rows="2" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <Grid v-if="dataForm.preHouseId" v-on:gridSelect="preGridSelect" alone="1" type="1" :house_id="dataForm.preHouseId"></Grid>
    <Grid v-if="dataForm.toHouseId" v-on:gridSelect="gridSelect" alone="1" type="1" :house_id="dataForm.toHouseId"></Grid>
    <template slot="footer">
      <el-divider></el-divider>
      <el-row>
        <el-col :span="24">
          <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
          <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>/* eslint-disable */
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      rows: [],
      columns: [],
      rows2: [],
      columns2: [],
      dataForm: {
        id: '',
        transferDate: '',
        preGrid: {row:'', column:''},
        preHouseId: '',
        toGrid: {row:'', column:''},
        toHouseId: '',
        mounts: '',
        weight: '',
        operator: '',
        age: '',
        remark: ''
      },
      pickerOptions: { },
      houses:{}
    }
  },
  computed: {
    dataRule () {
      return {
        transferDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        preGrid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        preHouseId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        toGrid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        toHouseId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mounts: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        age: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    gridSelect(grid) {
      console.table(grid)
      this.dataForm.toGrid.row = grid.row
      this.dataForm.toGrid.column = grid.column
      this.$forceUpdate()
    },
    preGridSelect(grid) {
      console.table(grid)
      this.dataForm.preGrid.row = grid.row
      this.dataForm.preGrid.column = grid.column
      this.$forceUpdate()
    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
        this.getHouses()
      })
    },
    house_change() {
      let house = this.houses[this.dataForm.preHouseId]
      this.rows = []
      this.columns = []
      for (let i=0; i<house.rows; ++i) {
        this.rows.push({
          value: i+1,
          label: i+1+'行'
        })
      }
      for (let i=0; i<house.columns; ++i) {
        this.columns.push({
          value: i+1,
          label: i+1+'列'
        })
      }
    },
    house_change2() {
      let house = this.houses[this.dataForm.toHouseId]
      this.rows2 = []
      this.columns2 = []
      for (let i=0; i<house.rows; ++i) {
        this.rows2.push({
          value: i+1,
          label: i+1+'行'
        })
      }
      for (let i=0; i<house.columns; ++i) {
        this.columns2.push({
          value: i+1,
          label: i+1+'列'
        })
      }
    },
    getHouses () {
      let that = this;
      this.$http.get(`/basic/house/all`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        res.data.forEach(function(house){
          that.houses[house.id] = house;
        });
        this.$forceUpdate()
      }).catch(() => {})
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/stock/piggrowth/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.dataForm.preGrid = this.dataForm.preGrid.row + '-' + this.dataForm.preGrid.column;
        this.dataForm.toGrid = this.dataForm.toGrid.row + '-' + this.dataForm.toGrid.column;
        this.$http[!this.dataForm.id ? 'post' : 'put']('/stock/piggrowth/transfer/', this.dataForm).then(({ data: res }) => {
          this.dataForm.preGrid = {row:'', column:''};
          this.dataForm.toGrid = {row:'', column:''};
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style scoped>
    .el-input, .el-select {
      width: 180px;
    }

    .el-textarea {
      width: 460px;
    }
</style>
